<template>
  <div id="viewOffice"></div>
</template>
<script>
// import { handleDocType } from '@/utils/constants'
import { handleDocType } from '../common/utils/doctype.js'
export default {
  props: {
    option: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      doctype: ''
    }
  },
  mounted() {
    if (this.option.url) {
      this.setEditor(this.option)
    }
  },
  methods: {
    setEditor(option) {
      // 自动解析文件类型
      this.doctype = handleDocType(option.fileType)
      console.log(this.doctype)
      // office配置参数
      let config = {
        document: {
          fileType: option.fileType,
          key: option.key,
          title: option.title,
          url: option.url,
          // info: {
          //   favorite: true
          // },
          permissions: {
            comment: false, // 允许注释
            download: false, // 允许下载
            modifyContentControl: false, // 允许修改控件
            modifyFilter: false, // 允许筛选控件
            fillForms: false,
            print: true, // 允许打印
            copy: true, // 允许复制
            protect: true,
            // review: true, // 允许修改视图样式
            edit: option.isEdit//是否可以编辑: 只能查看则传false
            // "fillForms": true,//是否可以填写表格，如果将mode参数设置为edit，则填写表单仅对文档编辑器可用。 默认值与edit或review参数的值一致。
            // "review": true //跟踪变化
          }
        },

        documentType: this.doctype, // 只有三大类, 不是文件后缀名, 见 constant.js
        editorConfig: {

          //actionLink: "",
          //createUrl: "",
          callbackUrl: option.editUrl,//"编辑word后保存时回调的地址，这个api需要自己写了，将编辑后的文件通过这个api保存到自己想要的位置

          // "embedded": {
          //   "embedUrl": "https://example.com/embedded?doc=exampledocument1.docx",
          //   "fullscreenUrl": "https://example.com/embedded?doc=exampledocument1.docx#fullscreen",
          //   "saveUrl": "https://example.com/download?doc=exampledocument1.docx",
          //   "shareUrl": "https://example.com/view?doc=exampledocument1.docx",
          //   "toolbarDocked": "top"
          // },

          // 语言设置
          lang: 'zh-CN',
          location: 'zh-CN',
          region: 'zh-CN',

          // "coEditing": {
          //   "mode": "fast",
          //   "change": true
          // },

          mode: "view",

          // "plugins": {
          //   "autostart": [
          //     "asc.{0616AE85-5DBE-4B6B-A0A9-455C4F1503AD}",
          //     "asc.{FFE1F462-1EA2-4391-990D-4CC84940B754}",
          //     ...
          //   ],
          //   "pluginsData": [
          //     "https://example.com/plugin1/config.json",
          //     "https://example.com/plugin2/config.json",
          //     ...
          //   ]
          // },

          // "recent": [
          //   {
          //     "folder": "Example Files",
          //     "title": "exampledocument1.docx",
          //     "url": "https://example.com/exampledocument1.docx"
          //   },
          //   {
          //     "folder": "Example Files",
          //     "title": "exampledocument2.docx",
          //     "url": "https://example.com/exampledocument2.docx"
          //   },
          //   ...
          // ],

          // "templates": [
          //   {
          //     "image": "https://example.com/exampletemplate1.png",
          //     "title": "exampletemplate1.docx",
          //     "url": "https://example.com/url-to-create-template1"
          //   },
          //   {
          //     "image": "https://example.com/exampletemplate2.png",
          //     "title": "exampletemplate2.docx",
          //     "url": "https://example.com/url-to-create-template2"
          //   },
          //   ...
          // ],

          // "user": {
          //   "group": "Group1",
          //   "id": "78e1e841",
          //   "name": "John Smith"
          // },

          // 自定义
          customization: {
            // anonymous: {
            //   request: false,
            //   label: "Guest"
            // },

            autosave: false, //是否自动保存
            chat: false, // 开启聊天插件
            forcesave: false,// true 表示强制文件保存请求添加到回调处理程序
            feedback: {
              // url: "https://example.com",
              visible: false // 隐藏反馈按钮
            },
            // "goback": {
            //   "blank": true,
            //   "requestClose": false,
            //   "text": "Open file location",
            //   "url": "https://example.com"
            // },
            comments: false,
            help: false,
            hideNotes: true,
            hideRulers: true,
            hideRightMenu: true,//定义在第一次加载时是显示还是隐藏右侧菜单。 默认值为false
            "macros": true,
            "macrosMode": "warn",
            "mentionShare": false,
            "plugins": false,
            spellcheck: false, //拼写检查
            // "toolbarHideFileName": false,
            // "toolbarNoTabs": false,
            // "uiTheme": "theme-dark",
            // "unit": "cm",
            // "zoom": 100,

            // "review": {
            //   "hideReviewDisplay": false,
            //   "showReviewChanges": false,
            //   "reviewDisplay": "original",
            //   "trackChanges": true,
            //   "hoverMode": false
            // },

            // "customer": {
            //   "address": "My City, 123a-45",
            //   "info": "Some additional information",
            //   "logo": "https://example.com/logo-big.png",
            //   "logoDark": "https://example.com/dark-logo-big.png",
            //   "mail": "john@example.com",
            //   "name": "John Smith and Co.",
            //   "www": "example.com"
            // },

            logo: {
              image: 'https://file.iviewui.com/icon/viewlogo.png',
              imageEmbedded: 'https://file.iviewui.com/icon/viewlogo.png'

              // "image": "https://example.com/logo.png",
              // "imageDark": "https://example.com/dark-logo.png",
              // "url": "https://example.com"
            }
          }
        },

        // "events": {
        //   "onAppReady": onAppReady,
        //   "onCollaborativeChanges": onCollaborativeChanges,
        //   "onDocumentReady": onDocumentReady,
        //   "onDocumentStateChange": onDocumentStateChange,
        //   "onDownloadAs": onDownloadAs,
        //   "onError": onError,
        //   "onInfo": onInfo,
        //   "onMetaChange": onMetaChange,
        //   "onOutdatedVersion": onOutdatedVersion,
        //   "onPluginsReady": onPluginsReady,
        //   "onRequestClose": onRequestClose,
        //   "onRequestCompareFile": onRequestCompareFile,
        //   "onRequestCreateNew": onRequestCreateNew,
        //   "onRequestEditRights": onRequestEditRights,
        //   "onRequestHistory": onRequestHistory,
        //   "onRequestHistoryClose": onRequestHistoryClose,
        //   "onRequestHistoryData": onRequestHistoryData,
        //   "onRequestInsertImage": onRequestInsertImage,
        //   "onRequestMailMergeRecipients": onRequestMailMergeRecipients,
        //   "onRequestRename": onRequestRename,
        //   "onRequestRestore": onRequestRestore,
        //   "onRequestSaveAs": onRequestSaveAs,
        //   "onRequestSendNotify": onRequestSendNotify,
        //   "onRequestSharingSettings": onRequestSaveAs,
        //   "onRequestUsers": onRequestUsers,
        //   "onWarning": onWarning
        // },

        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M",
        "type": "desktop",

        width: '60%',
        height: '100%'
      }
      // eslint-disable-next-line no-unused-vars,no-undef
      let docEditor = new DocsAPI.DocEditor('viewOffice', config)

    }
  },
  watch: {
    option: {
      // eslint-disable-next-line no-unused-vars
      handler: function(n, o) {
        this.setEditor(n)
        this.doctype = handleDocType(n.fileType)
      },
      deep: true
    }
  }
}
</script>

